package day06;

import jakarta.servlet.*;
import jakarta.servlet.annotation.WebListener;
import jakarta.servlet.http.HttpSession;
import jakarta.servlet.http.HttpSessionEvent;
import jakarta.servlet.http.HttpSessionListener;

@WebListener
public class CountListener implements HttpSessionListener {

    private Integer count=0;

    @Override
    public void sessionCreated(HttpSessionEvent event) {
        System.out.println("CountListener.sessionCreated()");

        HttpSession session=event.getSession();
        ServletContext application=session.getServletContext();
        synchronized(this){
            application.setAttribute("count", ++count);
        }
    }

    @Override
    public void sessionDestroyed(HttpSessionEvent event) {
        System.out.println("CountListener.sessionDestroyed()");

        HttpSession session=event.getSession();
        ServletContext application=session.getServletContext();
        synchronized(this){
            application.setAttribute("count", --count);
        }
    }
}
